package com.itheima.work1127;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class Demo03 {
    public static void main(String[] args) throws DocumentException {
        //创建解析器对象
        SAXReader read = new SAXReader();
        //通过解析器获取对象的document
        Document doc = read.read(new File("E:\\idea-project\\project002\\properties-xml-logback-app\\src\\book.xml"));
        //通过document对象获取根元素
        Element root = doc.getRootElement();
        //根据xml结构调佣对象方法
        List<Element> list = root.elements();

        //增强for循环
        for (Element element : list) {
            Attribute idAttr = element.attribute("id");
            String id = idAttr.getValue();

            Element child = element.element("name");
            String name = child.getText();

            Element pri = element.element("price");
            String price = pri.getText();

            Element da = element.element("date");
            String date = da.getText();

            Element ho = element.element("house");
            String house = ho.getText();
            System.out.println("id=" + id + ",书名=" + name + "，出版社" + house + "，日期" + date +",价格=" + price);

        }


    }
}
